const jsonic = require('jsonic')
const _ = require('lodash')

module.exports = function gatewayPlugin(config) {
  this.add(`service:${config.serviceName}`, (msg, respond) => {
    if (msg.args) {
      let pin = {}
      _.merge(pin, msg.args.params, msg.args.query, msg.args.body)
      const pattern = jsonic.stringify(pin)
      console.info(`消息：${pattern}`)
      this.act(pattern, function (error, result) {
        if (error) {
          return respond(error)
        }
        return respond(null, result)
      })
    } else {
      return respond(new Error('网关服务只开发http(s)协议!'))
    }
  })
}
